
const base64 = require('./base64.js');

var xorData={

    encryptionData:function(str,sendDic){

        return receiveDataDecode(str,sendDic)
       
    }

}

  const base64_encode = (str) => {
    return base64.Base64.encode(str);
  }

  const alice = (str, key) => { //xor加密

    let output = '';
    let newKey = key.split("").reverse().join(""); //逆序key
    let keyLen = newKey.length - 1; //忽略key最后一个字符
    for (let i = 0; i < str.length; i++) {
      let j = i % keyLen;
      output += String.fromCharCode(str.charCodeAt(i) ^ newKey.charCodeAt(j));
    }
    
    return output
  }
  const toAlice = (str, key) => {
      
    return base64_encode(alice(str, key));
  }
  
  const receiveDataDecode = (datastring,sendDic) =>{
   
    var postdata 

    console.log("解密的字典2",sendDic);

    var key 

    key = toAlice(sendDic["appid"], String(sendDic["appver"]));

     postdata= base64_encode(JSON.stringify(datastring))
 
    var sendData=toAlice(postdata,key)

    return sendData
 
  }

module.exports = xorData;